package com.qianfeng.client_7001.controller;

import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.netflix.hystrix.contrib.javanica.cache.annotation.CacheKey;
import com.netflix.hystrix.contrib.javanica.cache.annotation.CacheRemove;
import com.netflix.hystrix.contrib.javanica.cache.annotation.CacheResult;
import com.qianfeng.client_7001.client.OrderClient;
import com.qianfeng.client_7001.client.SearchClient;
import com.qianfeng.client_7001.client.SendClient;
import com.qianfeng.client_7001.client.SideCarClient;
import com.qianfeng.client_7001.entity.User;
import com.qianfeng.client_7001.service.AService;
import com.qianfeng.client_7001.service.CustomerService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * author: 田晋宇
 * datetime:2021/10/25 11:07
 * email:853796359@qq.com
 */
@RestController
@RefreshScope
public class CustomerController {

    @Qualifier("eurekaClient")
    @Autowired
    private EurekaClient eurekaClient;

    @Autowired
    private RestTemplate restTemplate;


    @Autowired
    private SearchClient searchClientclient;

    @Autowired
    private OrderClient orderClient;

    @Autowired
    private CustomerService customerService;

    @Autowired
    private SideCarClient carClient;

    @Autowired
    private SendClient sendClient;

    @Autowired
    private AService aService;


    @Value("${msg}")
    private String msg;

    @RequestMapping("/customer")
    public String helloCustomer(){


     /*   InstanceInfo ins = eurekaClient.getNextServerFromEureka("SEARCH", false);

        String url = ins.getHomePageUrl();
        System.out.println(url);
*/

        String forObject = restTemplate.getForObject( "http://SEARCH/search", String.class);
        return forObject;

    }

    @RequestMapping("/s1")
    public String search1(){

        aService.add();
        return "ok";
    }

    @HystrixCommand(fallbackMethod = "ss2",commandProperties = {
            @HystrixProperty(name = "circuitBreaker.enabled",value = "true"),
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "10"),
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "50"),
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "5000")
    })
    @RequestMapping("/sss2")
    public String sss2(Integer id){


        return searchClientclient.s1(id);
    }

    @RequestMapping("/s2")
    @HystrixCommand(fallbackMethod ="ss2")
    public String s2(Integer id){
        int i =1/0;
        return searchClientclient.s1(id);
    }

    public String ss2(Integer id){

        return "本地数据 服务降级";
    }

    @RequestMapping("/c3")
    @HystrixCommand(fallbackMethod = "cc3",commandProperties = {
            @HystrixProperty(name = "execution.isolation.strategy",value = "SEMAPHORE"),
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000")
    })
    public String c3(Integer id,String name) throws InterruptedException {

        System.out.println(Thread.currentThread().getName());//hystrix-CustomerController-1

        ///http-nio-7001-exec-1  tomcat 线程池名称
        Thread.sleep(4000);
        return  searchClientclient.s2(id,name);
    }
    public String cc3(Integer id,String name){

        return  "c3本地数据 远程服务已经超时";
    }

    @RequestMapping("/c4")
    public User c4(User user){
        System.out.println("c:"+user);
        return searchClientclient.s3(user);
    }

    @RequestMapping("/c5")
    public String c5( Integer id){
        String s1 = customerService.c5(id);
        String s2 = customerService.c5(id);
        customerService.clearCache(id);
        customerService.c5(id);
        customerService.c5(id);
        return "ok";
    }




    @RequestMapping("/bm")
    public String getBmInfo(){
        String api = carClient.api();
        return api;
    }

    @RequestMapping("/send")
    public String sendMsg(){

        sendClient.send().send(MessageBuilder.withPayload("cloud msg").build());
        return "ok";
    }

    @RequestMapping("/msg")
    public String msg(){
        return msg;
    }

    @RequestMapping("/order")
    public String order(){
        return orderClient.createOrder();
    }

}
